package info.nene_loveplus.controller.pub;

import info.nene_loveplus.model.blog.Entry;
import info.nene_loveplus.service.BlogService;
import info.nene_loveplus.util.WikiUtil;

import java.util.List;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

import com.google.appengine.repackaged.com.google.common.collect.Lists;
import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEnclosure;
import com.sun.syndication.feed.synd.SyndEnclosureImpl;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl;
import com.sun.syndication.io.SyndFeedOutput;
import com.totsp.xml.syndication.itunes.EntryInformation;
import com.totsp.xml.syndication.itunes.EntryInformationImpl;
import com.totsp.xml.syndication.itunes.FeedInformation;
import com.totsp.xml.syndication.itunes.FeedInformationImpl;
import com.totsp.xml.syndication.itunes.types.Category;
import com.totsp.xml.syndication.itunes.types.Duration;

public class RssController extends Controller {

	private BlogService blogService = new BlogService();

	@SuppressWarnings("unchecked")
	@Override
	public Navigation run() throws Exception {
		SyndFeed feed = new SyndFeedImpl();
		feed.setFeedType("rss_2.0");
		feed.setTitle("プログラミンガー思す");
		feed.setLink("http://podcast.programminger.com/");
		feed.setDescription("Programminger 0xb055");
		feed.setAuthor("dev7.jp");

		FeedInformation itunesFeed = new FeedInformationImpl();
		{
			itunesFeed.setBlock(false);
			itunesFeed.setExplicit(false);
			itunesFeed.setKeywords(new String[] { "プログラミンガー", "プログラマー",
					"プログラミング" });
			Category category = new Category("");
			itunesFeed.setCategory(category);
			itunesFeed.setOwnerEmailAddress("daishi.hmr@gmail.com");
			itunesFeed.setOwnerName("Daishi Narazaki");
		}
		feed.getModules().add(itunesFeed);

		List<SyndEntry> list = Lists.newArrayList();
		for (Entry entry : blogService.listAll()) {
			SyndEntry item = new SyndEntryImpl();
			item.setTitle(entry.getTitle());
			item.setLink("http://podcast.programminger.com/pub/single?key="
					+ entry.getKeyAsString());
			item.setPublishedDate(entry.getCreatedAt());
			SyndContent description = new SyndContentImpl();
			description.setType("text/html");
			description
					.setValue(WikiUtil.toHtml(entry.getContent().getValue()));
			item.setDescription(description);

			EntryInformation itunesItem = new EntryInformationImpl();
			{
				itunesItem.setAuthor("dev7.jp");
				itunesItem.setBlock(false);
				Duration duration = new Duration(0L);
				itunesItem.setDuration(duration);
				itunesItem.setExplicit(false);
			}
			item.getModules().add(itunesItem);

			if (entry.getAudioFileRef().getModel() != null) {
				SyndEnclosure enc = new SyndEnclosureImpl();
				{
					enc.setUrl(entry.getAudioFileRef().getModel().getUrl());
					enc.setType("audio/mpeg");
					enc.setLength(0L);
				}
				item.getEnclosures().add(enc);
			}

			list.add(item);
		}
		feed.setEntries(list);

		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Expires", "Thu, 01 Dec 1994 16:00:00 GM");
		response.setContentType("application/rss+xml; charset=utf-8");

		SyndFeedOutput out = new SyndFeedOutput();
		out.output(feed, response.getWriter());

		return null;
	}
}
